package com.example.demo.controller;

import com.example.demo.componet.ReadCSV;
import com.example.demo.model.BrandJson;
import org.apache.spark.api.java.JavaRDD;
import org.apache.spark.api.java.JavaSparkContext;
import org.apache.spark.api.java.function.Function2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/analyze")
public class DiffenctManWoman implements Serializable {

    @Resource
    private JavaSparkContext sc;

    //文件地址
    @Autowired
    private String getFilePath;

    private List<ArrayList> getMaps() {
        ReadCSV readCSV = new ReadCSV();
        //List<ArrayList> maps =
        return  readCSV.readTwoColumn(getFilePath, 7, 10);   //"C:\\Users\\fireapproval\\Desktop\\毕业设计\\数据集\\test.csv"
    }




    @GetMapping("/difference")
    public List<BrandJson> method2(){

        List<BrandJson> list = new ArrayList<>();
        List<Integer> man = new ArrayList<>() ;
        List<Integer> woman = new ArrayList<>();

        //System.out.println(maps);
        //System.out.println(maps.toString());

        for (Object obj:getMaps()
        ) {
            ArrayList a = (ArrayList) obj;

            Integer i = Math.round(Float.parseFloat((String) a.get(0)));
            //System.out.println(a.get(0)+"----"+a.get(1));
            //System.out.println(a.get(0)+"--"+a.get(0).getClass());  //4--class java.lang.String
            //System.out.println(a.get(1)+"--"+a.get(1).getClass());
            String s=(String) a.get(1);

            if ( s == "男" || s.equals("男")){
                man.add(i.intValue());
            }
            if (s == "女" || s.equals("女")) {
                woman.add(i.intValue());
            }

        }

        System.out.println("-----man"+ man);
        System.out.println("-----wom"+ woman);

        JavaRDD<Integer> manRdd = sc.parallelize(man);
        JavaRDD<Integer> woamRdd = sc.parallelize(woman);

        Integer reduce = manRdd.reduce((Function2<Integer, Integer, Integer>) (integer, integer2) -> integer + integer2);
        Integer reduce1 = woamRdd.reduce((Function2<Integer, Integer, Integer>) (integer, integer2) -> integer + integer2);

        BrandJson brandJson = new BrandJson();brandJson.setName("男");brandJson.setValue(reduce);
        BrandJson brandJson1 = new BrandJson();brandJson1.setName("女");brandJson1.setValue(reduce1);

        list.add(brandJson);list.add(brandJson1);

        return list;

    }
}
